﻿/*============================================================================
  File:    Program.cs

  Summary: 

  Date:    June 13, 2005
------------------------------------------------------------------------------
  This file is part of the Microsoft SQL Server Code Samples.

  Copyright (C) Microsoft Corporation.  All rights reserved.

  This source code is intended only as a supplement to Microsoft
  Development Tools and/or on-line documentation.  See these other
  materials for detailed information regarding Microsoft code samples.

  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  PARTICULAR PURPOSE.
============================================================================*/

#region Using directives

using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.AnalysisServices;

#endregion

namespace Microsoft.Samples.SqlServer
{
    class Program
    {
        /// <summary>
        /// Connects to an Analysis Services server and displays names of objects.
        /// </summary>
        /// <param name="args">The Analysis Services server to connect to (as args[0]).</param>
        static void Main(string[] args)
        {
            if ((null == args) || (args.Length != 1))
            {
                //--------------------------------------------------------------------------------
                // Display information on how to run this application.
                //--------------------------------------------------------------------------------
                Console.WriteLine();
                Console.WriteLine("USAGE:");
                Console.WriteLine("  DisplayObjectNames AnalysisServicesConnectionString");
                Console.WriteLine();
                Console.WriteLine("SAMPLE USAGE:");
                Console.WriteLine("  DisplayObjectNames localhost");
                Console.WriteLine("  DisplayObjectNames \"Data Source=localhost\"");
                Console.WriteLine();
            }
            else
            {
                //--------------------------------------------------------------------------------
                // We'll connect to the Analysis Services server specified by the command line 
                // parameter (args[0]) and we'll display the names of databases and other objects.
                //--------------------------------------------------------------------------------
                try
                {
                    Server server = new Server();

                    server.Connect(args[0]);

                    try
                    {
                        //--------------------------------------------------------------------------------
                        // For each database on the server, we'll display its name and other objects names.
                        //--------------------------------------------------------------------------------
                        foreach (Database db in server.Databases)
                        {
                            Console.WriteLine("{0}", db.Name);

                            // Display Dimensions
                            Console.WriteLine("\tDimensions:");
                            foreach (Dimension dimension in db.Dimensions)
                                Console.WriteLine("\t\t{0}", dimension.Name);

                            // Display Cubes
                            Console.WriteLine("\tCubes:");
                            foreach (Cube cube in db.Cubes)
                                Console.WriteLine("\t\t{0}", cube.Name);

                            // Display Mining Structures
                            Console.WriteLine("\tMining Structures:");
                            foreach (MiningStructure miningStructure in db.MiningStructures)
                                Console.WriteLine("\t\t{0}", miningStructure.Name);
                        }
                    }
                    finally
                    {
                        server.Disconnect();
                        server.Dispose();
                    }
                }
                catch (AmoException e)
                {
                    Console.WriteLine(e.ToString());
                }
            }
        }
    }
}
